package com.hyt.it.ogt.controller.pj;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.pj.common.Constants;
import com.hyt.it.ogt.pj.common.ResponseCode;
import com.hyt.it.ogt.pj.model.entity.UserConfig;
import com.hyt.it.ogt.pj.service.IUserConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;

/**
 * <p>
 * 用户配置表表 前端控制器
 * </p>
 *
 * @author qiaozj
 * @since 2020-07-09
 */
@ApiSort(value = 5)
@Api(tags = "5.用户配置接口", value = "用户配置接口")
@RestController
@RequestMapping("/user-config")
public class UserConfigController extends BaseController {

    @Autowired
    private IUserConfigService userConfigService;

    @ApiOperationSort(value = 1)
    @ApiOperation(value = "5.1.1根据id获取用户配置", notes = Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("query/{id}")
    public Object query(@PathVariable("id") String id){
        UserConfig userConfig = userConfigService.getById(id);
        if(userConfig != null){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(userConfig).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "5.2.1添加用户配置", notes= Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("add")
    public Object add(@RequestBody UserConfig userConfig){
        Boolean flag = userConfigService.save(userConfig);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "5.2.2设置用户配置", notes= "20000:成功")
    @GetMapping("setUserConfig")
    public Object setUserConfig(@RequestParam("projectId") String projectId){
        String userId = getUserId();
        Integer code = userConfigService.setUserConfig(userId, Constants.SELECTED_PROJECT_KEY, projectId);
        return ApiResponse.builder().code(code).build();
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "5.3.1更新用户配置", notes = Constants.SWAGGER_METHOD_NOTES)
    @PutMapping("update")
    public Object update(@RequestBody UserConfig userConfig){
        boolean flag = userConfigService.updateById(userConfig);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 30)
    @ApiOperation(value = "5.4.1根据id删除用户配置", notes = Constants.SWAGGER_METHOD_NOTES)
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") String id){
        boolean flag = userConfigService.removeById(id);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

}
